package com.csvw.skr.basic.filter;

import com.csvw.skr.basic.entity.UseSituation;
import com.webonn.frame.core.TokenController;
import com.webonn.frame.db.MyDao;
import com.webonn.frame.util.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * Created by Peng on 2017/8/11.
 */
public class UseSituationFilter implements Filter {

/*    private static ScheduledExecutorService tasks = new ScheduledThreadPoolExecutor(50);*/
    @Autowired
    private MyDao myDao;

    private boolean useSituation;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
   /*     Object object = filterConfig.getServletContext().getAttribute("myDao");
        if (object instanceof MyDao) {
            this.myDao = (MyDao) object;
        }
        useSituation = Boolean.valueOf(filterConfig.getInitParameter("useSituation"));*/
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

       /* if (useSituation) {
            tasks.execute(() -> {
                HttpServletRequest request = (HttpServletRequest) servletRequest;
                String remoteAddr = request.getHeader("X-Real-IP");
                String requestURI = request.getRequestURI();
                String method = request.getMethod();
                String token = request.getHeader("authorization").replace("Bearer ", "");
                String userId = TokenController.getInstance().getEntityFromToken(Objects.requireNonNull(token));

                UseSituation useSituation = new UseSituation();
                useSituation.setId(IdGenerator.generateUUID("use"));
                useSituation.setRemoteAddr(remoteAddr);
                useSituation.setRequestURI(requestURI);
                useSituation.setMethod(method);
                useSituation.setUserId(userId);
                useSituation.setCreated(new Date());

                myDao.persist(useSituation);
            });
        }*/
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String remoteAddr = request.getHeader("X-Real-IP");
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        String token = request.getHeader("authorization").replace("Bearer ", "");
        String userId = TokenController.getInstance().getEntityFromToken(Objects.requireNonNull(token));

        UseSituation useSituation = new UseSituation();
        useSituation.setId(IdGenerator.generateUUID("use"));
        useSituation.setRemoteAddr(remoteAddr);
        useSituation.setRequestURI(requestURI);
        useSituation.setMethod(method);
        useSituation.setUserId(userId);
        useSituation.setCreated(new Date());

        myDao.persist(useSituation);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
